package com.hwsc.pay.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisLockService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public boolean tryLock(String key) {
        return redisTemplate.opsForValue().setIfAbsent(key, "locked", 30, TimeUnit.SECONDS);
    }
    public boolean tryLock(String key, int expire, TimeUnit timeUnit) {
        return redisTemplate.opsForValue().setIfAbsent(key, "locked", expire, timeUnit);
    }
    public void releaseLock(String key) {
        redisTemplate.delete(key);
    }
}
